﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MachineQA
{
    /// <summary>
    /// Interaction logic for MachineTab.xaml
    /// </summary>
    public partial class MachineTab : UserControl
    {
        public MachineTab()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            MachinesListbox.DataContext = Constants.entities.Machines.Select((i) => i.Name).ToList();
            LocationCombobox.DataContext = Constants.entities.Locations.Select((i) => i.Name).ToList();
        }

        private void MachineSaveButton_Click(object sender, RoutedEventArgs e)
        {
             Machine newMachine = new Machine();
             newMachine.Name = NewMachineNameBox.Text;
             newMachine.Location = Constants.entities.Locations.Where((i) => i.Name == "SRCC").First();
             Constants.entities.AddToMachines(newMachine);
             try
             {
                 Constants.entities.SaveChanges();
                 MachineSaveButton.IsEnabled = false;
             }
             catch (Exception ex)
             {
                 MessageBox.Show("There was a problem loading the new machine. Error: \n" + ex.Message);
             }

             // Not perfect but works for now (Probably fine until 10's of items since the listbox is 
             // cleared and reloaded reasonably quick for a small number of elements. 
             // Need to have IObservable wrapper around entity context query ... how?
             MachinesListbox.DataContext = null;
             MachinesListbox.DataContext = Constants.entities.Machines.Select((i) => i.Name).ToList();
        }

        private void NewMachineNameBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TryEnableSaveButton();
        }

        private void TryEnableSaveButton()
        {
            MachineSaveButton.IsEnabled = (NewMachineNameBox.Text != string.Empty && (string)LocationCombobox.SelectedItem != null &&
                                          (Constants.entities.Machines.Where(
                                                                              (i) => i.Name == NewMachineNameBox.Text &&
                                                                              i.Location.Name == (string)LocationCombobox.SelectedValue
                                                                            ).Count() == 0));
        }

        private void LocationCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TryEnableSaveButton();
        }
    }
}
